using UnityEngine;

namespace Yoozoo.Gameplay.RTS
{
    public class SquareDirectorController : MonoBehaviour
    {
        public GameObject leftBottomCorner;
        public GameObject leftTopCorner;
        public GameObject rightTopCorner;
        public GameObject rightBottomCorner;
        
        public GameObject bottomBorder;
        public GameObject leftBorder;
        public GameObject rightBorder;
        public GameObject topBorder;

        private Vector3 leftBottomCornerOriginPos;
        private Vector3 leftTopCornerOriginPos;
        private Vector3 rightBottomCornerOriginPos;
        private Vector3 rightTopCornerOriginPos;

        private Vector3 bottomBorderOriginPos;
        private Vector3 leftBorderOriginPos;
        private Vector3 rightBorderOriginPos;
        private Vector3 topBorderOriginPos;

        private float cornerLength = 1.35f;
        private float borderLength = 2.74f;

        private void Awake()
        {
            leftBottomCornerOriginPos = leftBottomCorner.transform.localPosition;
            leftTopCornerOriginPos = leftTopCorner.transform.localPosition;
            rightBottomCornerOriginPos = rightBottomCorner.transform.localPosition;
            rightTopCornerOriginPos = rightTopCorner.transform.localPosition;

            bottomBorderOriginPos = bottomBorder.transform.localPosition;
            leftBorderOriginPos = leftBorder.transform.localPosition;
            rightBorderOriginPos = rightBorder.transform.localPosition;
            topBorderOriginPos = topBorder.transform.localPosition;
        }

        public void UpdateSize(float width, float height)
        {
            leftBottomCorner.transform.localPosition = new Vector3(leftBottomCornerOriginPos.x * width,leftBottomCornerOriginPos.y,leftBottomCornerOriginPos.z * height);
            leftTopCorner.transform.localPosition = new Vector3(leftTopCornerOriginPos.x * width,leftTopCornerOriginPos.y,leftTopCornerOriginPos.z * height);
            rightBottomCorner.transform.localPosition = new Vector3(rightBottomCornerOriginPos.x * width,rightBottomCornerOriginPos.y,rightBottomCornerOriginPos.z * height);
            rightTopCorner.transform.localPosition = new Vector3(rightTopCornerOriginPos.x * width,
                rightTopCornerOriginPos.y, rightTopCornerOriginPos.z * height);
            
            bottomBorder.transform.localScale = new Vector3(1,1,GetBorderScale(width));
            topBorder.transform.localScale = new Vector3(1,1,GetBorderScale(width));
            topBorder.transform.localPosition = new Vector3(topBorderOriginPos.x,
                topBorderOriginPos.y,
                leftTopCorner.transform.localPosition.z + (topBorderOriginPos.z - leftTopCornerOriginPos.z));
            leftBorder.transform.localPosition = new Vector3(
                leftBottomCorner.transform.localPosition.x + leftBorderOriginPos.x - leftBottomCornerOriginPos.x,
                leftBorderOriginPos.y,
                (leftBottomCorner.transform.localPosition.z + leftTopCorner.transform.localPosition.z) / 2);
            leftBorder.transform.localScale = new Vector3(1,1,GetBorderScale(height));
            rightBorder.transform.localPosition = new Vector3(
                rightBottomCorner.transform.localPosition.x + rightBorderOriginPos.x - rightBottomCornerOriginPos.x,
                rightBorderOriginPos.y,
                (rightBottomCorner.transform.localPosition.z + rightTopCorner.transform.localPosition.z) / 2);
            rightBorder.transform.localScale = new Vector3(1,1,GetBorderScale(height));
        }

        private float GetBorderScale(float length)
        {
            return (length - 2 * cornerLength * 0.182f) / (borderLength * 0.182f);
        }
        
    }
}